package com.sskj.hhhjys;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.ColorEnum;
import com.sskj.lib.bean.TabItem;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.KChartPop;
import com.sskj.lib.widget.SpinnerPop;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/10
 */
@Route(path = RConfig.APP_MARKET)
public class MarketDetailActivity extends BaseActivity<MarketDetailPresenter> {


    @Autowired
    String code;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.left_tv)
    TextView leftTv;
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.tvIndicator)
    TextView tvIndicator;
    @BindView(R.id.frameLayoutChart)
    FrameLayout frameLayoutChart;
    @BindView(R.id.tvUSDT)
    TextView tvUSDT;
    @BindView(R.id.tvHigh)
    TextView tvHigh;
    @BindView(R.id.tvRMB)
    TextView tvRMB;
    @BindView(R.id.tvRate)
    TextView tvRate;
    @BindView(R.id.tvLow)
    TextView tvLow;
    String[] goodsType = {"minute", "minute5", "minute15", "minute30", "minute60", "day"};
    private int currentIndex;
    private SpinnerPop spinnerPop;
    private KChartPop kChartPop;
    private boolean isUpToggle = true;
    private boolean isDownToggle = true;
    private ArrayList<ChartFragment> fragmentList = new ArrayList();

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_market_detail;
    }

    @Override
    public MarketDetailPresenter getPresenter() {
        return new MarketDetailPresenter();
    }

    @Override
    public void initView() {

        RxBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
        setTitle(code.replace("_","/").toUpperCase());
        ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
        customTabEntities.add(new TabItem(App.INSTANCE.getString(R.string.appJavaMarketActivity8), 0, 0));
        customTabEntities.add(new TabItem("5M", 0, 0));
        customTabEntities.add(new TabItem("15M", 0, 0));
        customTabEntities.add(new TabItem("30M", 0, 0));
        customTabEntities.add(new TabItem("1H", 0, 0));
        customTabEntities.add(new TabItem(App.INSTANCE.getString(R.string.appJavaMarketActivity13), 0, 0));
        commonTabLayout.setTabData(customTabEntities);
        initTab();
        initPoint();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.summary_content, (Fragment) ARouter.getInstance()
                .build(RConfig.APP_FRAGMENT_SUMMARY)
                .withString(Constans.CODE, code)
                .navigation()).commitAllowingStateLoss();
    }

    private void initPoint() {
        ClickUtil.click(tvIndicator, () -> {
            if (kChartPop == null) {
                kChartPop = new KChartPop(this);
                View contentView = kChartPop.getContentView();
                TextView tvMa = contentView.findViewById(R.id.tvMa);
                TextView tvBoll = contentView.findViewById(R.id.tvBoll);
                TextView tvMacd = contentView.findViewById(R.id.tvMacd);
                TextView tvKdj = contentView.findViewById(R.id.tvKdj);
                TextView tvRsi = contentView.findViewById(R.id.tvRsi);
                TextView tvWr = contentView.findViewById(R.id.tvWr);
                ImageView ivUpToggle = contentView.findViewById(R.id.ivUpToggle);
                ImageView ivDownToggle = contentView.findViewById(R.id.ivDownToggle);
                ivUpToggle.setImageResource(R.mipmap.app_icon_show);
                ivDownToggle.setImageResource(R.mipmap.app_icon_show);
                ClickUtil.click(tvMa, () -> {
                    fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().setMainDrawMaShow();
                    ivUpToggle.setImageResource(R.mipmap.app_icon_show);
                    kChartPop.dismiss();
                });
                ClickUtil.click(tvBoll, () -> {
                    fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().setMainDrawBollShow();
                    ivUpToggle.setImageResource(R.mipmap.app_icon_show);
                    kChartPop.dismiss();

                });
                ClickUtil.click(ivUpToggle, () -> {
                    if (isUpToggle) {
                        isUpToggle = false;
                        ivUpToggle.setImageResource(R.mipmap.app_icon_hide);

                        fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().setMainDrawNoneShow();
                    } else {
                        isUpToggle = true;
                        ivUpToggle.setImageResource(R.mipmap.app_icon_show);
                        fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().setMainDrawMaShow();
                    }
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvMacd, () -> {
                    fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().changeMACD();
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvKdj, () -> {
                    fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().changeKDJ();
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvRsi, () -> {
                    fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().changeRSI();
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvWr, () -> {
                    fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().changeWR();
                    kChartPop.dismiss();

                });
                ClickUtil.click(ivDownToggle, () -> {
                    if (isDownToggle) {
                        isDownToggle = false;
                        ivDownToggle.setImageResource(R.mipmap.app_icon_hide);

                        fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().setDrawDown(isDownToggle);
                    } else {
                        isDownToggle = true;
                        ivDownToggle.setImageResource(R.mipmap.app_icon_show);

                        fragmentList.get(commonTabLayout.getCurrentTab()).getKChartView().setDrawDown(isDownToggle);
                    }
                    kChartPop.dismiss();

                });
                kChartPop.setBackground(0);
            }
            if (kChartPop.isShowing()) {
                kChartPop.dismiss();
            } else {
                kChartPop.showPopupWindow(commonTabLayout);
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getData(code);
    }


    private void initTab() {
        fragmentList.add(null);
        fragmentList.add(null);
        fragmentList.add(null);
        fragmentList.add(null);
        fragmentList.add(null);
        fragmentList.add(null);

        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectTab(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        selectTab(0);
    }

    /**
     * k线切换
     *
     * @param index
     */
    private void selectTab(int index) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ChartFragment minuteFragment = fragmentList.get(index);
        ChartFragment currentFragment = fragmentList.get(currentIndex);
        if (currentFragment != null) {
            ft.hide(currentFragment);
        }
        if (minuteFragment == null) {
            if (index == 0) {
                minuteFragment = ChartFragment.newInstance(code, goodsType[index], true);
            } else {
                minuteFragment = ChartFragment.newInstance(code, goodsType[index], false);
            }
            fragmentList.set(index, minuteFragment);
            ft.add(R.id.frameLayoutChart, minuteFragment);
        } else {
            ft.show(minuteFragment);
        }
        ft.commit();
        currentIndex = index;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateUI(CoinBean data) {
        if (data.getCode().equals(code)) {
            tvUSDT.setText(data.getPrice());
            tvRMB.setText(String.format("≈%s CNY", CommonUtil.dealCNY(data.getCnyPrice())));
            tvRate.setText(data.getChangeRate());
            tvLow.setText(data.getLow());
            tvHigh.setText(data.getHigh());
            tvUSDT.setTextColor(ContextCompat.getColor(App.INSTANCE, data.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
            tvRate.setTextColor(ContextCompat.getColor(App.INSTANCE, data.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
        }
    }
}
